#-*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import lxml
import requests
import xlsxwriter


class Spider():
    # 初始化参数
    def __init__(self, urlList):
        self.urlList = urlList
        self.num = 0
        self.row = 0
        self.col = 0
        self.wookbook = ''
        self.wooksheet = ''
        self.id = 0
        self.sort = ''

    # 下载
    def downLoad(self, add_url):
        print('开始下载html')
        res = requests.get(add_url)
        html = res.text
        soup = BeautifulSoup(html, 'html5lib')
        a_list = soup.select('.listnav_dl_style1 dd a')
        for a in a_list:
            new_url_list = a.attrs['href']
            for num in range(1,57):
                print(num)
                if new_url_list=='https://www.meishij.net/china-food/caixi/chuancai/':
                    self.sort = '川菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/xiangcai/':
                    self.sort = '湘菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/yuecai/':
                    self.sort = '粤菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/dongbeicai/':
                    self.sort = '东北菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/lucai/':
                    self.sort = '鲁菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/zhecai/':
                    self.sort = '浙菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/sucai/':
                    self.sort = '苏菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/qingzhencai/':
                    self.sort = '清真菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/mincai/':
                    self.sort = '闽菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/hucai/':
                    self.sort = '沪菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/jingcai/':
                    self.sort = '京菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/hubeicai/':
                    self.sort = '湖北菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/huicai/':
                    self.sort = '徽菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/yucai/':
                    self.sort = '豫菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/xibeicai/':
                    self.sort = '西北菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/yuguicai/':
                    self.sort = '云贵菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/jiangxicai/':
                    self.sort = '江西菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/shancicai/':
                    self.sort = '山西菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/guangxicai/':
                    self.sort = '广西菜'
                elif new_url_list=='https://www.meishij.net/china-food/caixi/gangtai/':
                    self.sort = '港台菜'
                else:
                    self.sort = '其他菜'
                res = requests.get(new_url_list+'?&page='+str(num))
                html = res.text
                self.analy(html)

    # 解析html
    def analy(self, html):
        print('开始解析html')
        soup = BeautifulSoup(html, 'html5lib')
        food_name_list_str = soup.select('.c1 strong')
        #食物名字
        food_name_list = []
        for index in food_name_list_str:
            food_name_list.append(index.get_text())
        big_list = soup.select('.listtyle1 .big')
        efficacy_list_str = soup.select('.big .gx span')
         #功效列表
        efficacy_list = []
        for efficacy in efficacy_list_str:
            efficacy_list.append(efficacy.get_text())
        for index,get_big in enumerate(big_list):
            if len(get_big)!=7:
                efficacy_list.insert(index,'暂无功效')
        step_list_str = soup.select('.i .c2 .li1')
         #步骤
        step_list = []
        for index in step_list_str:
            step_list.append(index.get_text())
        taste_list_str = soup.select('.i .c2 .li2')
         #口味
        taste_list = []
        for index in taste_list_str:
            taste_list.append(index.get_text())
        url_list_str = soup.select('.big .img')
        # url
        url_list = []
        for index in url_list_str:
            url_list.append(index.attrs['src'])
        try:
            for food_name,efficacy,step,taste,url in zip(food_name_list,efficacy_list,step_list,taste_list,url_list):
                # 给excel中写入内容
                # 同时下载图片
                self.row +=1
                self.wooksheet.write(self.row, self.col, self.row)
                self.wooksheet.write(self.row, self.col+1, food_name)
                self.wooksheet.write(self.row, self.col+2, efficacy)
                self.wooksheet.write(self.row, self.col+3, step)
                self.wooksheet.write(self.row, self.col+4, taste)
                self.wooksheet.write(self.row, self.col+6, self.sort)
                imgContent = requests.get(url)
                with open('/home/wwwroot/default/python/img/'+str(self.row)+'.jpg', 'wb') as f:
                    f.write(imgContent.content)
                    self.wooksheet.write(self.row, self.col+5, str(self.row)+'.jpg')
        except:
            print('出错')
        
    # 创立一个excel表
    def setExcel(self):
        self.wookbook = xlsxwriter.Workbook('/home/wwwroot/default/python/getFood.xlsx')
        self.wooksheet = self.wookbook.add_worksheet()
        self.wooksheet.write(self.row, self.col, 'id')
        self.wooksheet.write(self.row, self.col+1, 'food_name')
        self.wooksheet.write(self.row, self.col+2, ' efficacy')
        self.wooksheet.write(self.row, self.col+3, ' step')
        self.wooksheet.write(self.row, self.col+4, ' taste')
        self.wooksheet.write(self.row, self.col+5, ' url')
        self.wooksheet.write(self.row, self.col+6, ' sort')

    # 主调度函数
    def main(self):
        self.setExcel()
        for url in self.urlList:
            self.downLoad(url)
        self.wookbook.close()


# 入口函数
if __name__ == "__main__":
    urlList = ["https://www.meishij.net/china-food/caixi/"]
    craw = Spider(urlList)
    craw.main()